package com.spddr.pmos.cxf;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import org.springframework.data.jpa.repository.Query;

import com.spddr.pmos.jpa.TradeWay;

import cn.seqdata.cxf.repos.JaxrsObjectRepository;

@Path("/tradeWay")
public interface JaxrsTradeWay extends JaxrsObjectRepository<TradeWay> {
	@PUT
	@Path("/")
	default <S extends TradeWay> S save(S entity) {
		Long entity_id = entity.getId();
		if(entity_id != null){
			return getRepository().save(entity);
		}else{
			List<TradeWay> tras = getRepository().findAll();
			List<Long> ids = new ArrayList<Long>();
			for(TradeWay trade:tras){
				ids.add(trade.getId());
			}
			Long maxid = Collections.max(ids);
			entity.setId(maxid+1);
			return getRepository().save(entity);
		}
		
	}
	
	@GET
	@Path("/{id}/children")
	List<TradeWay> children(@PathParam("id") long id);
	
	@GET
	@Path("/pid/{pid}")
	List<TradeWay> findFistChildren(@PathParam("pid") long pid);
}
